package imgscala;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Charsets;
import com.google.common.io.Files;
import com.raycloud.sdk.shopee.DefaultShopeeClient;
import com.raycloud.sdk.shopee.ShopeeClient;
import com.raycloud.sdk.shopee.request.ItemGetItemDetailRequest;
import com.raycloud.sdk.shopee.response.ItemGetItemDetailResponse;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.*;

/**
 * Created by donar on 17/12/15.
 */
public class ShopImageFixUtil {
    public static void main1(String[] args) throws Exception {
        ShopeeClient shopeeClient = new DefaultShopeeClient();
        List<String> formerImages = new ArrayList<String>();
        formerImages.addAll(Arrays.asList(new String[]{"https://cfshopeetw-a.akamaihd.net/file/afb57a7e16070744d70bf5b9fb6888a7",
                "https://cfshopeetw-a.akamaihd.net/file/dcfa1acde0958ed6fa391e46eb133caf",
                "https://cfshopeetw-a.akamaihd.net/file/9cabaed5fb0714092758d93102289062","https://cfshopeetw-a.akamaihd.net/file/9cabaed5fb0714092758d93102289062"}));
        ItemGetItemDetailRequest req = new ItemGetItemDetailRequest(24399430,10676,"afa34fd2c93404f20ed883da1013f6febf4a3e049d56546be6e76f3f3787e493");
        req.setItemId(732277181L);
        ItemGetItemDetailResponse rep = shopeeClient.execute(req);
        List<String> onlineImgs = rep.getItem().getImages();
        if(onlineImgs.size() == formerImages.size()) return;

        List<String> formerImgHashs = new ArrayList<String>();
        List<String> onlineImgHashs = new ArrayList<String>();
        ImagePHash ip = new ImagePHash();
        for (String f : formerImages){
            BufferedImage bi = ImageIO.read(new URL(f));
            String hash = ip.getHash(bi);
            formerImgHashs.add(hash);
        }
        for (String o : onlineImgs){
            BufferedImage bi = ImageIO.read(new URL(o));
            String hash = ip.getHash(bi);
            onlineImgHashs.add(hash);
        }

        LinkedHashMap<Integer,String> missingMap= new LinkedHashMap<Integer,String>();
        int total = formerImages.size();
        for (int i = 0,j=0; i < total; i++) {
            String f = formerImgHashs.get(i);
            if (j>onlineImgHashs.size()-1){
                missingMap.put(i,formerImages.get(i));
                continue;
            }
            String o = onlineImgHashs.get(j);
            ImagePHash p = new ImagePHash();
            boolean missing = p.distance(f,o)>=10;
            if (missing){
                missingMap.put(i,formerImages.get(i));
            }else {
                j++;
            }
        }
        System.out.println(missingMap);
        for(Map.Entry<Integer,String> map : missingMap.entrySet()){
            int index = map.getKey();
            String picUrl = map.getValue();
            //TODO 调用 插入方法
        }
        //System.out.println(JSON.toJSONString(rep.getItem()));
    }

    public static void main(String[] args) throws IOException {

        List<String> list = Files.readLines(new File("/Users/donar/Desktop/imgcluster.data"), Charsets.UTF_8);
        for (int i = 0; i < 100; i++) {
            list.addAll(Files.readLines(new File("/Users/donar/Desktop/imgcluster.data"), Charsets.UTF_8));
        }
        Long start = System.currentTimeMillis();


        Set<Pic> hash = new HashSet<Pic>();
        for (int i = 0; i < list.size(); i++) {
            String[] ss = list.get(i).split(",");
            hash.add(new Pic(ss[0],ss[1]));
        }
        System.out.println(System.currentTimeMillis()-start);
        System.out.println(hash);
    }
}
class Pic{
    private String url;
    private String hash;

    public Pic(String url, String hash) {
        this.url = url;
        this.hash = hash;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getHash() {
        return hash;
    }

    public void setHash(String hash) {
        this.hash = hash;
    }
    @Override
    public boolean equals(Object o) {
        return ImagePHash.distance(this.hash,((Pic)o).getHash())<10;
    }

    @Override
    public String toString() {
        return url;
    }

    @Override
    public int hashCode() {
        return hash != null ? hash.hashCode() : 0;
    }
}
